package com.example.privacy;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.privacy.Utils.SharedPreferencesUtils;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class InformationActivity extends AppCompatActivity {
    private EditText etidnumber;
    private EditText etname;
    private EditText etphoneNumber;
    private EditText etaddress;
    private Button commitbutton;
    private Toast toast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_information);
        etidnumber = (EditText)findViewById(R.id.input_idnumber);
        etname = (EditText)findViewById(R.id.input_name);
        etphoneNumber = (EditText)findViewById(R.id.input_phonenumber);
        etaddress = (EditText)findViewById(R.id.input_address);
        commitbutton = (Button)findViewById(R.id.commit);
        commitbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doCommit();
            }
        });
    }

    private void doCommit(){
        String idnumber = etidnumber.getText().toString();
        String name = etname.getText().toString();
        String phoneNumber = etphoneNumber.getText().toString();
        String address = etaddress.getText().toString();
        if(idnumber==null||idnumber.equals("")){
            Toast.makeText(this,"身份证号不可为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if(name==null||name.equals("")){
            Toast.makeText(this,"姓名不可为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if(phoneNumber==null||phoneNumber.equals("")){
            Toast.makeText(this,"电话号不可为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if(address==null||address.equals("")){
            Toast.makeText(this,"地址不可为空",Toast.LENGTH_SHORT).show();
            return;
        }
        CommitInformation(idnumber,name,phoneNumber,address);
    }


    private void CommitInformation(final String idnumber, final String name, final String phoneNumber, final String address){
        new Thread(new Runnable() {
            @Override
            public void run() {
//                final MediaType JSON = MediaType.get("application/json; charset=utf-8");
                String url = getString(R.string.baseUrl)+"/GetInformation";
                url = url + String.format("?IdNumber=%s&name=%s&phoneNumber=%s&address=%s", idnumber,name,phoneNumber,address);
                System.out.println(url);
                final OkHttpClient client = new OkHttpClient();
                RequestBody body = RequestBody.create(null,"");
                System.out.println(body.toString());
                final Request request = new Request.Builder()
                        .url(url)
                        .addHeader("Authorization","Bearer "+ SharedPreferencesUtils.getInstance(InformationActivity.this).getString("token", ""))
                        .post(body)
                        .build();
                try {
                    //判断请求是否成功，也就是判断登录是否成功
                    Response response = client.newCall(request).execute();
                    if(response.code()==200&&response.body().string().equals("success")){
                        //这里根据type选择要去的页面
                        String type = SharedPreferencesUtils.getInstance(InformationActivity.this).getString("type","");
                        if(type.equals("0")){
                            //服务器返回收件人是他的快递信息
                            //到收件人的页面
                            Intent i = new Intent(InformationActivity.this,CustomerMenu.class);
                            startActivity(i);
                        }else if(type.equals("1")){
                            //目前直接到创建订单页面
                            Intent i = new Intent(InformationActivity.this, SenderMenu.class);
                            startActivity(i);
                        }else if(type.equals("2")){
                            //获得
                            Intent i = new Intent(InformationActivity.this, DeliveryMenu.class);
                            startActivity(i);
                        }
                        InformationActivity.this.finish();
                    }else{
                        //完善不成功，继续完善并提交
                        showToast(InformationActivity.this,"信息完善不成功");
                    }
                }catch (Exception e){
                    System.out.println(e.getMessage().toString());
                }
            }
        }).start();
    }


    public void showToast(Context context, String text) {
        Looper myLooper = Looper.myLooper();
        if (myLooper == null) {
            Looper.prepare();
            myLooper = Looper.myLooper();
        }

        if (toast == null) {
            toast = Toast.makeText(context, text, Toast.LENGTH_LONG);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.show();
        if ( myLooper != null) {
            Looper.loop();
            myLooper.quit();
        }
    }
}